package com.csylh.controller;


import com.csylh.VO.ResultVO;
import com.csylh.VO.WasteInfoVO;
import com.csylh.VO.WasteVO;
import com.csylh.entity.WasteCategory;
import com.csylh.entity.WasteInfo;
import com.csylh.service.CategoryService;
import com.csylh.service.WasteInfoService;
import com.csylh.util.ResponseVOUtil;
import com.csylh.util.ResultVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by 留歌
 * Date: 2018/3/14 9:00
 * Description:
 */
@RestController
@RequestMapping("/user/waste")
public class UserWasteController {
    @Autowired
    private WasteInfoService wasteInfoService;
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/list")
    public ResultVO list() {
        //1. 查询所有的商品
        List<WasteInfo> wasteInfoList = wasteInfoService.findAll();

        //2. 查询类目(一次性查询)
        List<WasteCategory> productCategoryList = categoryService.getAllCategory();

        // 数据拼装
        List<WasteVO> wasteVOList=new ArrayList<>();
        for (WasteCategory wasteCategory:productCategoryList){
            WasteVO wasteVO=new WasteVO();
            wasteVO.setCategoryType(wasteCategory.getCategoryType());
            wasteVO.setCategoryName(wasteCategory.getCategoryName());

            List<WasteInfoVO> wasteInfoVOList=new ArrayList<>();
            for(WasteInfo wasteInfo:wasteInfoList){
                if(wasteInfo.getCategoryType() == (wasteCategory.getCategoryType())){
                    WasteInfoVO wasteInfoVO=new WasteInfoVO();
                    BeanUtils.copyProperties(wasteInfo,wasteInfoVO);
                    wasteInfoVOList.add(wasteInfoVO);
                }
            }
            wasteVO.setWasteInfoVOList(wasteInfoVOList);
            wasteVOList.add(wasteVO);
        }
        return ResultVOUtil.success(wasteVOList);
    }

}
